package es.albarregas.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

import es.albarregas.beans.DireccionBean;
import es.albarregas.modelo.Direccion;

public class DireccionDAO extends ConexionDAO {
	
	ResultSet consulta = null;
    String query;

	private static final String INSERT_STATEMENT = 
			"insert into direccion (nombre, apellidos, telefono, email, direccion, codigopostal,"
			+ "poblacion, provincia, user_oid, pedido_oid) values(?,?,?,?,?,?,?,?,?,?);";

	
	public int insertarDireccion(Direccion d)
    {
        int id = -1;
        try
        {
            getConexion();
            preparedStatement = conexion.prepareStatement(INSERT_STATEMENT);
            
            preparedStatement.setString(1, d.getNombre());
			preparedStatement.setString(2, d.getApellidos());
			preparedStatement.setString(3, d.getTelefono());
			preparedStatement.setString(4, d.getEmail());
			preparedStatement.setString(5, d.getDireccion());
			preparedStatement.setString(6, d.getCodigoPostal());
			preparedStatement.setString(7, d.getPoblacion());
			preparedStatement.setString(8, d.getProvincia());
			preparedStatement.setInt(9, d.getOid());
			preparedStatement.setInt(10, d.getPedido_oid());
			
			preparedStatement.executeUpdate();
		
			preparedStatement = conexion.prepareStatement("SELECT max(oid) FROM direccion");
            consulta = preparedStatement.executeQuery();
            consulta.next();
            id = consulta.getInt("max(oid)");
        }
        catch (SQLException ex){
            Logger.getLogger(DireccionDAO.class.getName()).log(Level.SEVERE, null, ex);
        }catch(Exception e){
                Logger.getLogger(DireccionDAO.class.getName()).log(Level.SEVERE, null, e);
        }
        return id;
    }
	
	public int insertarDireccion(DireccionBean d)
    {
        int id = -1;
        try
        {
            getConexion();
            preparedStatement = conexion.prepareStatement(INSERT_STATEMENT);
            
            preparedStatement.setString(1, d.getNombre());
			preparedStatement.setString(2, d.getApellidos());
			preparedStatement.setString(3, d.getTelefono());
			preparedStatement.setString(4, d.getEmail());
			preparedStatement.setString(5, d.getDireccion());
			preparedStatement.setString(6, d.getCodigoPostal());
			preparedStatement.setString(7, d.getPoblacion());
			preparedStatement.setString(8, d.getProvincia());
			preparedStatement.setInt(9, d.getOid());
			preparedStatement.setInt(10, d.getPedido_oid());
			
			preparedStatement.executeUpdate();
		
			preparedStatement = conexion.prepareStatement("SELECT max(oid) FROM direccion");
            consulta = preparedStatement.executeQuery();
            consulta.next();
            id = consulta.getInt("max(oid)");
        }
        catch (SQLException ex){
            Logger.getLogger(DireccionDAO.class.getName()).log(Level.SEVERE, null, ex);
        }catch(Exception e){
                Logger.getLogger(DireccionDAO.class.getName()).log(Level.SEVERE, null, e);
        }
        return id;
    }
	
}
